package edu.uni.dormitory.service.impl;

import edu.uni.dormitory.bean.AccomodateApply;
import edu.uni.dormitory.bean.AccomodateApplyApproval;
import edu.uni.dormitory.bean.AccomodateApplyExample;
import edu.uni.dormitory.mapper.AccomodateApplyApprovalMapper;
import edu.uni.dormitory.mapper.AccomodateApplyMapper;
import edu.uni.dormitory.service.AccommodateService;
import edu.uni.userBaseInfo1.bean.ApprovalStepIncharge;
import edu.uni.userBaseInfo1.bean.Student;
import edu.uni.userBaseInfo1.service.ApprovalMainService;
import edu.uni.userBaseInfo1.service.ApprovalStepInchargeService;
import edu.uni.userBaseInfo1.service.EmployeeService;
import edu.uni.userBaseInfo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 郑伟森
 * @create 2019.05.01
 * @function 服务
 */
@Service
public class AccommodateServiceImpl implements AccommodateService{
    @Autowired
    private AccomodateApplyMapper accomodateApplyMapper;
    @Autowired
    private AccomodateApplyApprovalMapper accomodateApplyApprovalMapper;
    @Autowired
    private ApprovalMainService approvalMainService;
    @Autowired
    private ApprovalStepInchargeService approvalStepInchargeService;

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private StudentService studentService;

    @Override
    public AccomodateApply selectAccommodateApplyById(Long id, Long byWho) {
        //查询的同时判断是不是登录人申请的
        AccomodateApplyExample accomodateApplyExample = new AccomodateApplyExample();
        accomodateApplyExample.createCriteria().andIdEqualTo(id).andByWhoEqualTo(byWho);
        List<AccomodateApply> accomodateApplies = accomodateApplyMapper.selectByExample(accomodateApplyExample);
        return accomodateApplies.size()>0?accomodateApplies.get(0):null;
    }

    @Override
    public List<AccomodateApply> selectAccommodateApplyPageBySelf(Long loginId) {
        //查询的同时判断是不是登录人申请的
        AccomodateApplyExample accomodateApplyExample = new AccomodateApplyExample();
        accomodateApplyExample.createCriteria().andByWhoEqualTo(loginId);
        return accomodateApplyMapper.selectByExample(accomodateApplyExample);
    }

    @Override
    public List<AccomodateApply> selectAccommodateApplyPageByChild(Long childId) {
        //查询的同时判断是不是登录人申请的
        AccomodateApplyExample accomodateApplyExample = new AccomodateApplyExample();
        accomodateApplyExample.createCriteria().andByWhoEqualTo(childId);
        return accomodateApplyMapper.selectByExample(accomodateApplyExample);
    }

    @Override
    public List<AccomodateApply> selectAccommodateApplyPageByCollege(Long collegeId) {
        return accomodateApplyMapper.selectAccommodatePageByCollege(collegeId);
    }

    @Override
    public List<AccomodateApply> selectAccommodateApplyPageByArea(String areaId) {
        return accomodateApplyMapper.selectAccommodatePageByArea(areaId);
    }

    @Override
    public AccomodateApply selectAccommodateApplyByIdWithInstructor(Long id, Long collegeId) {
        //判断记录的申请人是不是属于这个学院，如果是才可以查看
        return accomodateApplyMapper.selectSameCollegeById(id,collegeId);
    }

    @Override
    public AccomodateApply selectAccommodateApplyByIdWithSettlementCenter(Long id) {
        return accomodateApplyMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int insertAccommodateApply(AccomodateApply accomodateApply, Long byWho) {
        List<Student> studentList = studentService.selectByUserId(byWho);
        long applyPerson = (studentList==null||studentList.size()==0)?-1:studentList.get(0).getId();
        if(applyPerson == -1) return -1;
        accomodateApply.setApplyPerson(applyPerson);
        accomodateApply.setApplyDate(new Date());
        //学校id直接写死好了
        Long mainId  = approvalMainService.selectIdByName((long)1,"accommodate");
        accomodateApply.setApprovalMainId(mainId);
        accomodateApply.setByWho(byWho);
        accomodateApply.setDatetime(new Date());
        System.out.println(accomodateApply);
        int i = accomodateApplyMapper.insert(accomodateApply);
        if(i > 0){
            //成功需要获取新建记录的id
            Long applyId = accomodateApply.getId();
            //申请算在审核步骤里
            //通过ApprovalMainId获取step=1的记录的id
            Long stepId = getStepIdByMainId(mainId,1);

            if(updateAccommodateApply(applyId,(byte)1,byWho,stepId) > 0L){
                return 1;
            }
        }
        return 0;
    }


    @Override
    public int updateAccommodateApplyWithFamilyHead(Long applyId, Byte agree, Long loginId) {
        //学校id直接写死好了
        Long mainId  = approvalMainService.selectIdByName((long)1,"accommodate");
        //通过ApprovalMainId获取step=2的记录的id
        Long stepId = getStepIdByMainId(mainId,2);
        if(updateAccommodateApply(applyId,agree,loginId,stepId)>0L){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateAccommodateApplyWithInstructor(Long applyId, Byte agree, Long loginId) {
        if(accomodateApplyApprovalMapper.selectLatestByApplyId(applyId).getStatus() == 0){
            return -1;
        }
        //学校id直接写死好了
        Long mainId  = approvalMainService.selectIdByName((long)1,"accommodate");
        Long stepId = null;
        if(agree == 1){
            //通过ApprovalMainId获取step=3的记录的id
            stepId = getStepIdByMainId(mainId,3);
        }else {
            stepId = getStepIdByMainId(mainId,0);
        }
        if(updateAccommodateApply(applyId,agree,loginId,stepId)>0L){
            return 1;
        }
        return 0;
    }

    @Override
    public String selectAccommodateStatusByApplyId(Long applyId) {
        //获取最新的approvel_step_incharge_id
        AccomodateApplyApproval accomodateApplyApproval = accomodateApplyApprovalMapper.selectLatestByApplyId(applyId);
        System.out.println(accomodateApplyApproval);
        Long stepId = accomodateApplyApproval.getApprovelStepInchargeId();
        //通过stepId获取步骤名
        return approvalStepInchargeService.selectById(stepId).getName();
    }

    @Override
    public List<AccomodateApply> selectAccommodateApplyList() {
        return accomodateApplyMapper.selectByExample(new AccomodateApplyExample());
    }

    @Override
    public int updateAccommodateApplyWithCenter(Long applyId, Byte agree, Long loginId) {
        if(accomodateApplyApprovalMapper.selectLatestByApplyId(applyId).getStatus() == 0){
            return -1;
        }
        //学校id直接写死好了
        Long mainId  = approvalMainService.selectIdByName((long)1,"accommodate");
        Long stepId = null;
        if(agree == 1){
            //通过ApprovalMainId获取step=3的记录的id
            stepId = getStepIdByMainId(mainId,4);
        }else {
            stepId = getStepIdByMainId(mainId,0);
        }
        if(updateAccommodateApply(applyId,agree,loginId,stepId)>0L){
            return 1;
        }
        return 0;
    }


    private Long updateAccommodateApply(Long applyId, Byte agree, Long loginId, Long stepId) {
        //新增accomodate_apply_approval记录
        AccomodateApplyApproval accomodateApplyApproval = new AccomodateApplyApproval();
        accomodateApplyApproval.setApplyId(applyId);
        accomodateApplyApproval.setCheckPerson(loginId);
        accomodateApplyApproval.setDealTime(new Date());
        accomodateApplyApproval.setStatus(agree);
        accomodateApplyApproval.setApprovelStepInchargeId(stepId);
        System.out.println(accomodateApplyApproval);
        int i = accomodateApplyApprovalMapper.insert(accomodateApplyApproval);

        if(i > 0){
            System.out.println(accomodateApplyApproval);
            return accomodateApplyApproval.getId();
        }
        return 0L;
    }


    private Long getStepIdByMainId(Long mainId,Integer step) {
        List<ApprovalStepIncharge> approvalStepIncharges = approvalStepInchargeService.selectByMainId(mainId);
        for(ApprovalStepIncharge a: approvalStepIncharges){
            if(a.getStep().equals(step)){
                return a.getId();
            }
        }
        return null;
    }

}
